import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpRequest } from '../../../../service/httpRequest';
import { GlobalVariable } from '../../../../global.config';
import { ModalDirective } from 'ng2-bootstrap/modal';
declare let $:any;
@Component({
  selector: 'app-device-group',
  templateUrl: './device-group.component.html',
  styleUrls: ['./device-group.component.css'] 
})
export class DeviceGroupComponent implements OnInit {
  deviceId:string = '';
  groupData:any[];
  pointmsg:string = '';  
  pid:any;
  tool = GlobalVariable.tooltip;
  constructor(private activeRoute:ActivatedRoute,private httpRequest:HttpRequest) {}

  ngOnInit() {
    this.activeRoute.params.subscribe(params=>{      
      if(params['deviceId'] == 'none') {
        this.deviceId = '';
      }
      else{
        this.deviceId = params['deviceId'];
        this.pid = params['deviceId'].split('|')[0];
        //console.log(this.pid)
      }     
      this.getdata();
    }); 
    
  }


  getdata(){
    this.httpRequest.sendPost('device_group_association/groupAll',{
      device_id:this.deviceId.replace('|','/')      
    }).subscribe(res=>{
     // console.log(res)
      if(res.code==700){
        this.pointmsg = res.msg;
      }else{

        this.groupData = res;
      }
     
    });
  }


  checkAll(ev){
      this.groupData.forEach(x => x.show = ev.target.checked)
  }

  isAllChecked() {   
    if(this.groupData){
      return this.groupData.every(_ => _.show);
    }    
  }

  comfirmdata(){

      let didstr=Array();
      for(var i=0;i<this.groupData.length;i++){
          if(this.groupData[i].show){
              didstr.push(this.groupData[i].id)
          }          
      }
      
      this.httpRequest.sendPost('device_group_association/update',{
        device_id:this.deviceId.replace('|','/'),
        group_id:didstr.join(',')||''     
      }).subscribe(res=>{
        //console.log(res)
        if(res.errCode==0){
          alert('设备分组成功');
          this.getdata();
        }
      });
    
  }





                

 
}
